﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Manager
{
    public class AssetBundleManager:AssetManagerBase
    {
        //游戏中所有的assetbundle
        private Dictionary<string, AssetBundle> assetBundleDic = new Dictionary<string, AssetBundle>();
        //高加载队列
        private List<AssetBundleLoaderEntity> HighEntityList = new List<AssetBundleLoaderEntity>();
        //中加载队列
        private List<AssetBundleLoaderEntity> MiddleEntityList = new List<AssetBundleLoaderEntity>();
        //低加载队列
        private List<AssetBundleLoaderEntity> LowEntityList = new List<AssetBundleLoaderEntity>();
        //当前正在加载
        private AssetBundleLoaderEntity currentEntity;

        //根据级别拿到对应的队列
        private List<AssetBundleLoaderEntity> getLoadQueue(BundleLoadLevel LoadLevel)
        {
            switch (LoadLevel)
            {
                case BundleLoadLevel.height:
                    return HighEntityList;
                case BundleLoadLevel.middle:
                    return MiddleEntityList;
                case BundleLoadLevel.low:
                    return LowEntityList;
            }
            return LowEntityList;
        }

        /// <summary>
        /// 加载assetbundle接口
        /// </summary>
        /// <param name="assetBundlePathList"></param>
        /// <param name="LoadLevel"></param>
        /// <param name="progress"></param>
        /// <param name="callBack"></param>
        public void LoadAssetBundle(List<string> assetBundlePathList, BundleLoadLevel LoadLevel,Action<float> progress ,Action Complete)
        {
            List<string> needDownLoadList = new List<string>();
            foreach (string path in assetBundlePathList)
            {
                if (!assetBundleDic.ContainsKey(path) || assetBundleDic[path] == null)
                    needDownLoadList.Add(path);
            }

            AssetBundleLoaderEntity entity = new AssetBundleLoaderEntity(needDownLoadList, LoadLevel, OnLoadComplete, Complete, progress);
            getLoadQueue(LoadLevel).Add(entity);

            if (currentEntity == null)
            {
                currentEntity = entity;
                currentEntity.StartLoad();
            }
        }

        //加载完成
        private void OnLoadComplete(AssetBundleLoaderEntity entity)
        {
            currentEntity = null;

            foreach (var asset in entity.GetAssetBundleDic())
            {
                assetBundleDic[asset.Key] = asset.Value;
            }

            if (getLoadQueue(entity.loadLevel).Contains(entity))
            {
                getLoadQueue(entity.loadLevel).Remove(entity);
            }

            entity.backOut();
            entity.Destory();

            entity = null;

            if(HighEntityList.Count > 0)
            {
                currentEntity = HighEntityList[0];
                HighEntityList.Remove(currentEntity);
                currentEntity.StartLoad();
                return;
            }

            if (MiddleEntityList.Count > 0)
            {
                currentEntity = MiddleEntityList[0];
                MiddleEntityList.Remove(currentEntity);
                currentEntity.StartLoad();
                return;
            }

            if (LowEntityList.Count > 0)
            {
                currentEntity = LowEntityList[0];
                LowEntityList.Remove(currentEntity);
                currentEntity.StartLoad();
                return;
            }

         
        }

        /// <summary>
        /// 卸载assetbundle接口
        /// </summary>
        public void RemoveAssetBundle(List<string> pathList, bool unloadAllloadedObjects)
        {
            foreach (string path in pathList)
            {
                RemoveAssetBundle(path, unloadAllloadedObjects);
            }
        }

        public void RemoveAssetBundle(string assetBundlePath, bool unloadAllloadedObjects)
        {
            if (assetBundleDic.ContainsKey(assetBundlePath))
            {
                assetBundleDic[assetBundlePath].Unload(unloadAllloadedObjects);
                assetBundleDic[assetBundlePath] = null;
                assetBundleDic.Remove(assetBundlePath);
            }
        }

        public AssetBundle GetAssetBundle(string path)
        {
            if (assetBundleDic.ContainsKey(path))
                return assetBundleDic[path];
            return null;
        }

    }

}
